package com.javaweb.servlet.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class MyHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        System.out.println("session创建了");
        //session对象创建了
        ServletContext application=event.getSession().getServletContext();
        Object onlinecount=application.getAttribute("onlinecount");
        if (onlinecount==null) {
            application.setAttribute("onlinecount",1);
        }else {
            int count02=(Integer)onlinecount;
            count02++;
            application.setAttribute("onlinecount",count02);

        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        //session销毁了
        ServletContext application=event.getSession().getServletContext();
        Integer onlinecount=(Integer) application.getAttribute("onlinecount");
        onlinecount--;
        application.setAttribute("onlinecount",onlinecount);
    }
}
